package day06

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable

object AccWordCount {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("accu").setMaster("local[*]")
    val sc = new SparkContext(conf)

    val rdd1: RDD[String] = sc.makeRDD(List("a","c","a","d","a","c"),2)

    //调用累加器对象
    val acc = new MyWCAcc
    //注册累加器
    sc.register(acc,"wdAcc")
    //开始分布式累加
    rdd1.foreach(acc.add(_))
    println(acc.value)

    // 把结果排序
    val res= sc.parallelize(acc.value.toList).sortBy(_._2).collect().toBuffer
    println(res)

  }
}
